package cn.SiFuLan.dao.Impl;

import cn.SiFuLan.dao.ISpuDao;
import cn.SiFuLan.entity.SpuEntity;
import cn.SiFuLan.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SpuDaoImpl extends BaseDao implements ISpuDao {
    @Override
    public boolean saveGoods(SpuEntity spu) {
        String sql = "INSERT INTO spu(Spu_Name,title,Class_id,Brand_id,Spu_Info,Packing,service,Takedown,AddTime,UpdateTime)VALUES (?,?,?,?,?,?,?,?,?,?)";
        List<Object> sz = new ArrayList<>();
        sz.add(spu.getSpu_Name());
        sz.add(spu.getTitle());
        sz.add(spu.getClass_id());
        sz.add(spu.getBrand_id());
        sz.add(spu.getSpu_Info());
        sz.add(spu.getPacking());
        sz.add(spu.getService());
        sz.add(spu.getTakedown());
        sz.add(spu.getAddTime());
        sz.add(spu.getUpdateTime());
        return executeUpdate(sql,sz);
    }

    @Override
    public int saveGoodsReId(SpuEntity spu) {
        String sql = "INSERT INTO spu(Spu_Name,title,Class_id,Brand_id,Spu_Info,Packing,service,Takedown,AddTime,UpdateTime)VALUES (?,?,?,?,?,?,?,?,?,?)";
        List<Object> sz = new ArrayList<>();
        sz.add(spu.getSpu_Name());
        sz.add(spu.getTitle());
        sz.add(spu.getClass_id());
        sz.add(spu.getBrand_id());
        sz.add(spu.getSpu_Info());
        sz.add(spu.getPacking());
        sz.add(spu.getService());
        sz.add(spu.getTakedown());
        sz.add(spu.getAddTime());
        sz.add(spu.getUpdateTime());
        executeUpdate(sql,sz);
        int id =0;
        String sql1 = "select max(Spu_id) id from spu";
        ResultSet rs = executeQuery(sql1,null);
        try {
            while (rs.next()){
                id = rs.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return id;
    }

    @Override
    public SpuEntity getSpuById(int spuid) {
        SpuEntity spu = null;
        String sql = "select * from spu where Spu_id=?";
        List<Object> sz = new ArrayList<>();
        sz.add(spuid);
        ResultSet rs = executeQuery(sql,sz);
        try {
            while(rs.next()){
                spu = new SpuEntity();
                spu.setSpu_id(rs.getInt("Spu_id"));
                spu.setSpu_Name(rs.getString("Spu_Name"));
                spu.setTitle(rs.getString("title"));
                spu.setClass_id(rs.getInt("Class_id"));
                spu.setBrand_id(rs.getInt("Brand_id"));
                spu.setSpu_Info(rs.getString("Spu_Info"));
                spu.setPacking(rs.getString("Packing"));
                spu.setService(rs.getString("service"));
                spu.setTakedown(rs.getInt("Takedown"));
                spu.setAddTime(rs.getString("AddTime"));
                spu.setUpdateTime(rs.getString("UpdateTime"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                CloseAll(rs.getStatement().getConnection(),rs.getStatement(),rs);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return spu;
    }
}
